package com.sk.weichat.ui.groupchat;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.bean.SearchUserAndRoomBean;
import com.sk.weichat.helper.DialogHelper;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.ui.message.MucChatActivity;
import com.sk.weichat.ui.message.search.SearchUserAndRoomActivity;
import com.sk.weichat.util.AppUtils;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.sk.weichat.view.MaxTextTwoLengthFilter;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * @Author: chenzl
 * @ClassName: ApplyForAddGroupActivity
 * @Description: 申请加入群
 * @CreateDate: 2019/9/16 15:39
 */
public class ApplyForAddGroupActivity extends EditBaseActivity implements View.OnClickListener {

    private TextView mTitleTv;
    private TextView tv_title_right;
    private ClearEditText edit_apply_content;
    private TextView tv_input_length;
    private String content;

    private String RoomId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_for_add_group);
        getSupportActionBar().hide();

        RoomId = getIntent().getStringExtra("RoomId");
        initView();
    }

    private void initView(){
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitleTv = findViewById(R.id.tv_title_center);
        mTitleTv.setText(getString(R.string.apply_add_group));
        tv_title_right = findViewById(R.id.tv_title_right);
        tv_title_right.setText(getString(R.string.send));
        tv_title_right.setOnClickListener(this);

        edit_apply_content = findViewById(R.id.edit_apply_content);
        tv_input_length = findViewById(R.id.tv_input_length);


        content = "大家好，我是"+coreManager.getSelf().getNickName()+"，欢迎大家随时撩我~";
        edit_apply_content.addTextChangedListener(new MyTextWatcher());
        edit_apply_content.setText(content);
        edit_apply_content.setSelection(content.length());
        edit_apply_content.setFilters(new InputFilter[]{new MaxTextTwoLengthFilter(60)});
    }

    @Override
    public void onClick(View v) {
        if (v == tv_title_right) {
            if (TextUtils.isEmpty(edit_apply_content.getText().toString())) {
                ToastUtil.showToast(this, "请输入内容");
            } else {
                roomApplyJoin(edit_apply_content.getText().toString());
            }
        }
    }

    private class MyTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0) {
                tv_title_right.setTextColor(getResources().getColor(R.color.color_FFBDC2C8));
                tv_title_right.setEnabled(false);
                tv_input_length.setText("0/" + 60);
            } else {
                if (!s.equals(content)) {
                    tv_title_right.setTextColor(getResources().getColor(R.color.deep_purple));
                    tv_title_right.setEnabled(true);
                }
                tv_input_length.setText(AppUtils.judgeTextLength(s.toString()) + "/" + 60);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    /**
     * @author: czl
     * @description 申请加入群
     * @date: 2019/9/18 16:06
     */
    private void roomApplyJoin(String reason){
        Map<String, String> params = new HashMap<>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("roomId", RoomId);
        params.put("reason",reason);
        DialogHelper.showDefaulteMessageProgressDialog(ApplyForAddGroupActivity.this);

        HttpUtils.post().url(coreManager.getConfig().ROOM_APPLY_JOIN)
                .params(params).build().execute(new BaseCallback<String>(String.class) {
            @Override
            public void onResponse(ObjectResult<String> result) {
                DialogHelper.dismissProgressDialog();
                if (result.getResultCode()==1){//加入成功 直接进群
                    ToastUtil.showToast(ApplyForAddGroupActivity.this,"发送成功");
                    finish();
                }else {
                    ToastUtil.showToast(ApplyForAddGroupActivity.this,"发送失败，请稍后重试");
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                DialogHelper.dismissProgressDialog();
                ToastUtil.showErrorNet(ApplyForAddGroupActivity.this);
            }
        });
    }
}
